#include<iostream>
#include<fstream>
#include<string.h>
#include<cstring>
#include"sqlite3.h"
#include"json.h"

#pragma comment(lib, "sqlite3.lib")
//#pragma comment(lib, "json_vc71_libmtd.lib")
#include"DB.h"
using namespace std;
Json::Value root;

//插入
bool AuthenticationBean::Insert() 
{
	sqlite3* db;
	int nResult = sqlite3_open("test.db", &db);
	char n[100], p[100];
	string u, pa;
	cout << "输入用户名 " << endl;
	cin >> n;
	cout << "输入密码" << endl;
	cin >> p;
	//u = this.userName;
	char* errmsg;
	string strSql;
	strSql += "insert into account values('";
	strSql.append(n);
	strSql += "','";
	strSql.append(p);
	strSql += "',0);";
	cout << strSql << endl;
	nResult = sqlite3_exec(db, strSql.c_str(), NULL, NULL, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(db);
		cout << "创建失败： " << sqlite3_errmsg(db) << endl;
		//return;
	}
	else
	{
		sqlite3_close(db);
		cout << "创建成功" << endl;
	}
	return 1;
}

//删除
bool AuthenticationBean::Delete() 
{
	sqlite3* db;
	int nResult = sqlite3_open("test.db", &db);
	char n[100], p[100];
	string u, pa;
	cout << "输入用户名 " << endl;
	cin >> n;
	cout << "输入密码" << endl;
	cin >> p;
	//u = this.userName;
	char* errmsg;
	string strSql;
	strSql += "delete from account where username = '";
	strSql.append(n);
	strSql += "' and password = '";
	strSql.append(p);
	strSql += "';";
	cout << strSql << endl;
	nResult = sqlite3_exec(db, strSql.c_str(), NULL, NULL, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(db);
		cout << "删除失败： " << sqlite3_errmsg(db) << endl;
		//return;
	}
	else
	{
		sqlite3_close(db);
		cout << "删除成功"<< endl;
	}
	return 1;
}

//更换密码
bool AuthenticationBean::Update() 
{
	sqlite3* db;
	int nResult = sqlite3_open("test.db", &db);
	char n[100], p[100], np1[100], np2[100];
	string u, pa;
	cout << "输入用户名 " << endl;
	cin >> n;
	cout << "输入密码" << endl;
	cin >> p;
	cout << "输入新密码 " << endl;
	cin >> np1;
	cout << "确认新密码 " << endl;
	cin >> np2;
	if (strcmp(np1,np2)!=0)
	{
		cout << "2次输入密码不一致 " << endl;
		return 1;
	}
	//u = this.userName;
	char* errmsg;
	string strSql;
	strSql += "update account set password='";
	strSql.append(np1);
	strSql += "' where username = '";
	strSql.append(n);
	strSql += "' and password = '";
	strSql.append(p);
	strSql += "';";
	cout << strSql << endl;
	nResult = sqlite3_exec(db, strSql.c_str(), NULL, NULL, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(db);
		cout << "修改失败： " << sqlite3_errmsg(db) << endl;
		//return;
	}
	else
	{
		sqlite3_close(db);
		cout << "修改成功" << endl;
	}
	return 1;
}

//身份验证
Json::Value AuthenticationBean::Query(Json::Value query)
{

	Json::Value response;
	sqlite3* db;
	int nResult = sqlite3_open("test.db", &db);
	int nRow, nCol;
	char** pResult;
	int s;
	//char n[100], p[100];

	/*string u, pa;
	cout << "输入用户名" << endl;
	cin >> n;
	cout << "输入密码" << endl;
	cin >> p;*/
	//u = this.userName;

	std::string n = query["username"].asString();
	std::string p = query["password"].asString();
	s = atoi(query["source"].asString().c_str());

	char* errmsg;
	string strSql;
	strSql += "select password from account where username = '";
	strSql.append(n);
	//strSql += "' and password = '";
	//strSql.append(p);
	strSql += "';";
	cout << strSql << endl;
	nResult = sqlite3_get_table(db, strSql.c_str(), &pResult, &nRow, &nCol, &errmsg);

	if (nRow == 0)
	{
		sqlite3_close(db);
		cout << "登录失败" << endl;

		Json::Value response;
		Json::StreamWriterBuilder wbuilder;
		//wbuilder.settings_["indentation"] = "\t";
		response["time"] = 24;
		response["type"] = 1;
		response["destination"] = 0;
		response["response_type"] = 4;
		response["roomId"] = -1;
		response["result"] = 1;		//no
		std::string document = Json::writeString(wbuilder, response);

		sqlite3_free_table(pResult);
		ofstream os;
		os.open("response.json");
		os << document;
		os.close();
		return response;
	}

	string strl;
	//char res[100];
	sqlite3_stmt* stmt = NULL;
	strl += "select loc from account where username = '";
	strl.append(n);
	//strSql += "' and password = '";
	//strSql.append(p);
	strl += "';";
	//int re = sqlite3_get_table(db, strSql.c_str(), &pResult, &nRow, &nCol, &errmsg);
	int result = sqlite3_prepare_v2(db, strl.c_str(), -1, &stmt, NULL);
	sqlite3_step(stmt);
	int loc = sqlite3_column_int(stmt, 0);
	cout << loc << endl;

	if (loc != s || pResult[1] != p)
	{
		sqlite3_close(db);
		cout << "登录失败" << endl;

		Json::Value response;
		Json::StreamWriterBuilder wbuilder;
		//wbuilder.settings_["indentation"] = "\t";
		response["time"] = 24;
		response["type"] = 1;
		response["destination"] = 0;
		response["response_type"] = 4;
		response["roomId"] = -1;
		response["result"] = 1;		//no
		std::string document = Json::writeString(wbuilder, response);

		sqlite3_free_table(pResult);
		ofstream os;
		os.open("response.json");
		os << document;
		os.close();
		return response;
	}
	else
	{
		sqlite3_close(db);
		cout << "登录成功" << endl;
		
		if (loc == 0)	//房客
		{
			string strl;
			//char res[100];
			sqlite3_stmt* stmt = NULL;
			strl += "select roomId from account where username = '";
			strl.append(n);
			strl += "';";
			//int re = sqlite3_get_table(db, strSql.c_str(), &pResult, &nRow, &nCol, &errmsg);
			int result = sqlite3_prepare_v2(db, strl.c_str(), -1, &stmt, NULL);
			sqlite3_step(stmt);
			int di = sqlite3_column_int(stmt, 0);
			cout << di << endl;

			//Json::Value response;
			Json::StreamWriterBuilder wbuilder;
			//wbuilder.settings_["indentation"] = "\t";
			response["time"] = 24;
			response["type"] = 1;
			response["destination"] = 0;
			response["response_type"] = 4;
			response["roomId"] = di;
			response["result"] = 0;
			std::string document = Json::writeString(wbuilder, response);

			sqlite3_free_table(pResult);
			ofstream os;
			os.open("response.json");
			os << document;
			os.close();
		}
		else if (loc == 1)		//前台
		{
			Json::StreamWriterBuilder wbuilder;
			wbuilder.settings_["roomId"] = "\t";
			response["time"] = 24;
			response["type"] = 1;
			response["destination"] = 1;
			response["response_type"] = 4;
			//response["roomId"] = -1;
			response["result"] = 0;
			std::string document = Json::writeString(wbuilder, response);

			sqlite3_free_table(pResult);
			ofstream os;
			os.open("response.json");
			os << document;
			os.close();
		}
		else if (loc == 2)		//经理
		{
			Json::StreamWriterBuilder wbuilder;
			wbuilder.settings_["roomId"] = "\t";
			response["time"] = 24;
			response["type"] = 1;
			response["destination"] = 2;
			response["response_type"] = 4;
			//response["roomId"] = -1;
			response["result"] = 0;
			std::string document = Json::writeString(wbuilder, response);

			sqlite3_free_table(pResult);
			ofstream os;
			os.open("response.json");
			os << document;
			os.close();
		}
		else if (loc == 3)		//管理员
		{
			Json::StreamWriterBuilder wbuilder;
			wbuilder.settings_["roomId"] = "\t";
			response["time"] = 24;
			response["type"] = 1;
			response["destination"] = 3;
			response["response_type"] = 4;
			//response["roomId"] = -1;
			response["result"] = 0;
			std::string document = Json::writeString(wbuilder, response);

			sqlite3_free_table(pResult);
			ofstream os;
			os.open("response.json");
			os << document;
			os.close();
		}
	}
	sqlite3_close(db);
	return response;
}


//int main()
//{
//	int i;
//	AuthenticationBean d;
//	while (true)
//	{
//		cout << "1:创建" << endl;
//		cout << "2:删除" << endl;
//		cout << "3:修改" << endl;
//		cout << "4:“登录”" << endl;
//		cin >> i;
//		switch (i)
//		{
//		case 1:
//			d.Insert();
//			break;
//		case 2:
//			d.Delete();
//			break;
//		case 3:
//			d.Update();
//			break;
//		case 4:
//			d.Query();
//			break;
//		}
//	}
//}